/**
 * Created by zheng on 2018/7/2.
 */
$(function () {
    //var schedule=0;
    $('#dg').datagrid({
        url: '/clientDevelopmentPlan/list',
        fit: true,
        fitColumns: true,
        //分页条
        pagination: true,
        // fit:true,
        //工具栏
        toolbar: "#tb",
        //单选
        singleSelect: true,
        columns: [/*[
         {field: 'baseInfo', title: '基本属性', colspan: 2, width: 100, align: "center"},
         {field: 'otherInfo', title: '其他属性', colspan: 3, width: 100, align: "center"}
         ],*/ [
            {field: 'x', checkbox: 'true'},
            {field: 'id', title: '编码', width: 100},
            {field: 'operationDate', title: '创建时间', width: 100, sortable: true, order: 'desc'},
            {field: 'modificationDate', title: '修改时间', width: 100, sortable: true, order: 'desc'},
            {
                field: 'originator', title: '创建人', width: 100,
                formatter: function (value, row, index) {
                    return value ? value.realname : "未知用户";
                }
            },
            {
                field: 'client', title: '客户姓名', width: 100,
                formatter: function (value, row, index) {
                    return value ? value.name : "未知客户";
                }
            },
            {field: 'subject', title: '开发主题', width: 100},
            {field: 'project', title: '详细计划', width: 100},
            {field: 'comments', title: '备注', width: 100},
            {
                field: 'schedule', title: '开发进度', width: 100,
                formatter: function (value, row, index) {
                    if (value == 0) {
                       // schedule=value;
                        return "<font color='red'>失败</font>";

                    } else if (value == 1) {
                       // schedule=value;
                        return "<font color='green'>开发中</font>";
                    }
                    else if (value == 2) {
                       // schedule=value;
                        return "<font color='blue'>成功</font>";
                    }
                }, width: 100, sortable: true, order: 'desc'
            }
        ]],
        //点击行时响应事件
        onClickRow: function (index, row) {

            if (row.schedule == 0) {
                $("#saveRecord").linkbutton({
                    text: "确认开发失败"
                })
            } else if (row.schedule == 1) {
                $("#saveRecord").linkbutton({
                    text: "驳回开发计划"
                })
            } else if (row.schedule == 2) {
                $("#saveRecord").linkbutton({
                    text: "确认开发成功"
                })
            }

        },
        //新增
        border:false,
        cls:"table table-very table-basic"
    })

    //准备输入的对话弹框
    $('#dd').dialog({
        title: '编辑开发计划',
        width: 700,
        height: 650,
        closed: true,
        // cache: false,
        modal: true,
        buttons: "#bb",
        //新增
        top:100

    })

    //函数处理
    var cmdObj;
    cmdObj = {
        //刷新页面
        reload: function () {
            $('#dg').datagrid("load")
        },
        /*
         无添加按钮,此处项目完成后需要删除
         //添加
         add: function () {
         //清除以前的数据
         $('#ff').form("clear");
         $('#dd').dialog("open")
         $('#dd').dialog("setTitle", "员工添加")
         },*/
        //改变开发进度
        changState: function () {
            var row = $('#dg').datagrid("getSelected");
            if (row == null) {
                $.messager.alert("温馨提示", "请选择数据");
                return;
            }
            $.get("/clientDevelopmentPlan/changState", {id: row.id}, function (data) {
                if (data.succeed) {
                    $.messager.alert("温馨提示", "更新状态成功");
                    $('#dg').datagrid("reload")
                } else {
                    $.messager.alert("温馨提示", "删除状态失败");
                }
            })
        },
        edit: function () {

            var row = $('#dg').datagrid("getSelected");
            if (row == null) {
                $.messager.alert("温馨提示", "请选择数据");
                return;
            }
            //console.log(row);
            //清除以前的数据
            $('#ff').form("clear");
            //打开弹窗
            $('#dd').dialog("open")
            $('#dd').dialog("setTitle", "编辑开发计划")
            //显示客户名
            row["client.name"]=row.client.name;
            //回显
            $('#ff').form('load', row);
        },
        //高级查询
        query: function () {
            var keyword = $("#keyword").textbox("getValue");
            var beginDate = $("#beginDate").datebox("getValue");
            var endDate = $("#endDate").datebox("getValue");
            var queryForschedule = $("#queryForschedule").textbox("getValue");
            //防止类型转换异常
            if(!queryForschedule){
                queryForschedule=-2;
            }
            $('#dg').datagrid('load', {
                keyword: keyword,
                queryForschedule: queryForschedule,
                beginDate: beginDate,
                endDate: endDate
            });
        },
        //保存数据[按钮]
        save: function () {
            var url = "/clientDevelopmentPlan/saveOrUpdate";
            //提交表单
            $('#ff').form('submit', {
                url: url,
                success: function (data) {
                    //把字符串转换为json
                    var data = $.parseJSON(data);
                    if (!data.succeed) {
                        $.messager.alert('温馨提示', data.errorMsg);
                    } else {
                        $.messager.alert('温馨提示', "保存成功");
                        //关闭对话框
                        $('#dd').dialog("close");
                        //刷新datagrid
                        $('#dg').datagrid("reload")
                    }
                }
            });
        },
        //关闭按钮
        cancel: function () {
            $('#dd').dialog("close")
        },
        //保存记录按钮
        saveRecord:function () {
            var row = $('#dg').datagrid("getSelected");
            deleteRecord=function (row) {
                var deleteId=row.id;
                $.get( "/clientDevelopmentPlan/delete",{'id':deleteId},function (data) {
                    if (!data.succeed) {
                        $.messager.alert('温馨提示', data.errorMsg);
                    } else {
                        $.messager.alert('温馨提示', "操作成功");
                        //关闭对话框
                        $('#dd').dialog("close");
                        //刷新datagrid
                        $('#dg').datagrid("reload")
                    }
                })
            }
            //保存记录方法
            saveRecord1=function (row,newStatus){
                console.log(row);
                $("#clientDevelopmentclientId").val(row.originator.id);
                var clientidfor=row.client.id;
                var originatoridfor=row.originator.id;
                var status=newStatus;
                //提交
               $.post( "/clientRecord/save",{'client.id':clientidfor,'newOustomerManager.id':originatoridfor,'status':status},function (data) {
                   if (!data.succeed) {
                       $.messager.alert('温馨提示', data.errorMsg);
                   } else {
                       $.messager.alert('温馨提示', "操作成功");
                       //关闭对话框
                       $('#dd').dialog("close");
                       //刷新datagrid
                       $('#dg').datagrid("reload")
                   }
               })


            }

            console.log(row);
            if(row.schedule==1){
                $.messager.confirm('确认','此开发计划将会永久删除,无法恢复!请确认操作!',function(r){
                    if (r){
                        deleteRecord(row);
                    }else{
                        return;
                    }
                });
            }
            if(row.schedule==0){
                $.messager.confirm('确认','开发记录将会保存到开发失败记录中,才计划将从此页面删除!请确认操作!',function(r){
                    if (r){
                        saveRecord1(row,3);
                    }else{
                        return;
                    }
                });
            }
            if(row.schedule==2){
                $.messager.confirm('确认','开发记录将会保存到开发成功记录中,该受开发的客户会成为正式客户,此计划将从此页面删除!请确认操作!',function(r){
                    if (r){
                        saveRecord1(row,2);
                    }else{
                        return;
                    }
                });
            }

        }


    };

    //调用函数集合对象
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })
})




